import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { getServerAuthSession } from "@/auth";

export async function POST(req: Request) {
  const session = await getServerAuthSession();
  const userId = (session as any)?.user?.id ?? null;
  const body = await req.json();
  const { postId, dwellMs = 0, device, scene } = body ?? {};
  if (!postId) return NextResponse.json({ error: "缺少postId" }, { status: 400 });
  await prisma.view.create({ data: { postId, userId, dwellMs: Number(dwellMs) || 0, device, scene } });
  return NextResponse.json({ ok: true });
}